//
//  VerifyCodeBtn.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/13.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "VerifyCodeBtn.h"
@interface VerifyCodeBtn()
/// 总倒计时时长
@property (nonatomic, assign) NSInteger curInterval;
/// <#注释#>
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation VerifyCodeBtn


- (void)configVerifyBtn:(NSInteger)interval{
    self.interval = interval;
    [self setTitle:LCLocalString(@"send_ver_code") forState:(UIControlStateNormal)];
    
    [self addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)click:(UIButton *)sender{
    if (self.clickBlock) {
        self.clickBlock();
    }
    
    
}

- (void)beginRun{
    
    self.userInteractionEnabled = NO;
    self.curInterval = self.interval;
    [self setTitle:[NSString stringWithFormat:@"%zd %@",self.curInterval, LCLocalString(@"hcxhq")] forState:(UIControlStateNormal)];
    LRWeakSelf(self);
    self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
       
        weakself.curInterval = weakself.curInterval - 1;
        
        if (weakself.curInterval == 0) {
            if (weakself.endBlock) {
                weakself.endBlock();
            }
            weakself.userInteractionEnabled = YES;
            [weakself setTitle:LCLocalString(@"send_ver_code") forState:(UIControlStateNormal)];
            [weakself.timer invalidate];
        }else{
            [weakself setTitle:[NSString stringWithFormat:@"%zd%@",self.curInterval,LCLocalString(@"hcxhq")] forState:(UIControlStateNormal)];
        }
        
    }];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

@end
